package cn.iocoder.yudao.module.biz.service.htPlan;

import cn.iocoder.yudao.framework.common.util.string.StringUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlanDetail.HtPlanDetailDO;
import cn.iocoder.yudao.module.biz.dal.mysql.htPlanDetail.HtPlanDetailMapper;
import cn.iocoder.yudao.module.biz.job.planExe.HtPlanExeJoinVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.htPlan.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlan.HtPlanDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.htPlan.HtPlanConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.htPlan.HtPlanMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 隐患排查计划 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HtPlanServiceImpl implements HtPlanService {

    @Resource
    private HtPlanMapper htPlanMapper;
    @Resource
    private HtPlanDetailMapper htPlanDetailMapper;

    @Override
    public String createHtPlan(HtPlanCreateReqVO createReqVO) {
        // 插入
        HtPlanDO htPlan = HtPlanConvert.INSTANCE.convert(createReqVO);
        //查询当前登录用户
        LoginUser user= SecurityFrameworkUtils.getLoginUser();
        //设置部门、用户ID
        htPlan.setUserId(user.getId());
        htPlan.setDeptId(user.getDeptId());
        htPlanMapper.insert(htPlan);
        List<HtPlanDetailDO> planDetailList=createReqVO.getPlanDetail();
        for(HtPlanDetailDO detail:planDetailList){
            detail.setPlanId(htPlan.getId());
//            detail.setRiskState(0);
        }
        htPlanDetailMapper.insertBatch(planDetailList);

        // 返回
        return htPlan.getId();
    }

    @Override
    public void updateHtPlan(HtPlanUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateHtPlanExists(updateReqVO.getId());
        // 更新
        HtPlanDO updateObj = HtPlanConvert.INSTANCE.convert(updateReqVO);
        htPlanMapper.updateById(updateObj);

        List<HtPlanDetailDO> planDetailList=updateReqVO.getPlanDetail();
        for(HtPlanDetailDO detail:planDetailList){
            String detailId=detail.getId();
            if(StringUtils.isEmpty(detailId)){
                detail.setPlanId(updateObj.getId());
//                detail.setRiskState(0);
                htPlanDetailMapper.insert(detail);
            }else{
                htPlanDetailMapper.updateById(detail);
            }
        }
    }

    @Override
    public void deleteHtPlan(String id) {
        // 校验存在
        this.validateHtPlanExists(id);
        // 删除
        htPlanMapper.deleteById(id);
    }

    private void validateHtPlanExists(String id) {
        if (htPlanMapper.selectById(id) == null) {
            throw exception(HT_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public HtPlanDO getHtPlan(String id) {
        return htPlanMapper.selectById(id);
    }

    @Override
    public List<HtPlanRiskVO> queryRiskByPlan(String id) {
        return htPlanMapper.queryByPlan(id);
    }

    @Override
    public List<HtPlanJoinPageVO> selectHtPlanPage(HtPlanPageReqVO pageVO) {
        return htPlanMapper.selectHtPlanPage(pageVO);
    }

    @Override
    public Integer selectHtPlanPageCount(HtPlanPageReqVO pageVO) {
        return htPlanMapper.selectHtPlanPageCount(pageVO);
    }

    @Override
    public List<HtPlanDO> getHtPlanList(Collection<String> ids) {
        return htPlanMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<HtPlanDO> getHtPlanPage(HtPlanPageReqVO pageReqVO) {
        return htPlanMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HtPlanDO> getHtPlanList(HtPlanExportReqVO exportReqVO) {
        return htPlanMapper.selectList(exportReqVO);
    }

    @Override
    public List<HtPlanExeJoinVO> listPlanExe(Map<String, Object> map) {
        return htPlanMapper.listPlanExe(map);
    }
}
